<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="commontaglib"%>
<%
String path = request.getContextPath();
%>


 
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">	
	<link rel="stylesheet" href="<%=path%>/css/zTreeStyle/zTreeStyle.css" type="text/css">
	<link rel="stylesheet" href="<%=path%>/css/zTreeStyle/demo.css" type="text/css">
	<link rel="stylesheet" href="<%=path%>/css/innerstyle.css" type="text/css" />
	<link rel="stylesheet" type="text/css" media="screen" href="<%=path%>/css/ui-lightness/jquery-ui-1.8.23.custom.css" />
	<script type="text/javascript" src="<%=path%>/js/jquery-1.7.2.min.js"></script>
	<script type="text/javascript" src="<%=path%>/js/jquery-ui-1.8.23.custom.min.js"></script> 
	<script type="text/javascript" src="<%=path%>/js/jquery.ztree.all-3.4.min.js"></script>	
	<style type="text/css">
	#but1{
	position: relative;
    right: 5px;
	top: 153px;
	text-align: center;
	}
	#but2{
	position: relative;
    right: 5px;
	top: 173px;
	text-align: center;
	}
	
	
	
	</style>
	<SCRIPT type="text/javascript">
		<!--
		var setting = {
			edit: {
				enable: false,
				showRemoveBtn: false,
				showRenameBtn: false
			},
			  data: {
			simpleData: {
				enable:true,
				idKey: "id",
				pIdKey: "pid",
				rootPId: ""
			}
		},
			callback: {			    
				beforeDrag: false,
				beforeDrop: false,
				onDblClick: zTreeOnDblClick
			}
		};

function zTreeOnDblClick(event, treeId, treeNode) {
   // alert(treeNode ? treeNode.tId + ", " + treeNode.name : "isRoot");
    //alert(treeId);
    if(treeId=='treeDemo'){	
		save(treeNode.id);       	
		}else{
		remove(treeNode.id); 
		}	
};

		function beforeDrag(treeId, treeNodes) {
			for (var i=0,l=treeNodes.length; i<l; i++) {
			    //alert(treeNodes[i].pId);
				if (treeNodes[i].drag === false) {
					return false;
				}
			}
			return true;
		}
		function beforeDrop(treeId, treeNodes, targetNode, moveType) {
		    //alert(treeNodes[0].name);
			return targetNode ? targetNode.drop !== false : true;
		}
		
		function chickDrop(treeID,type){		
		var treeObj = $.fn.zTree.getZTreeObj(treeID);		
		var sNodes = treeObj.getSelectedNodes();       
	    var tId = sNodes[0].tId;
        var node = treeObj.getNodeByTId(tId);   

		if(type=='1'){	
		save(node.id);       	
		}else{
		remove(node.id); 
		}				
		}
		
		function save(nodeid){		    
		    var url = "<%=path%>/jydz/saveFriendsInfo.action?nodeid="+nodeid;		    
		    $.post(url, function(data) {
               if(eval('(' + data + ')').result=='1'){
		        alert("操作成功!");
		       }else{
		         alert("操作失败！");
		       }
               new_data();
            });
		}
		
		function remove(nodeid){
		   var url = "<%=path%>/jydz/removeFriendsInfo.action?nodeid="+nodeid;		   
		    $.post(url, function(data) {
               if(eval('(' + data + ')').result=='1'){
		        alert("操作成功!");
		       }else{
		         alert("操作失败！");
		       }
               new_data();
            });
		}
		
		function old_data(){
		$.ajax({
             type: "post",
             dataType:'text',
             //dataType: "json",//返回json格式的数据
             url: "<%=path%>/jydz/findFriends.action",
             success: function(data){
                 var zNodes = data;                 
                 $.fn.zTree.init($("#treeDemo"), setting, eval('(' + zNodes + ')')); 
                }  
              });
		
		}
		
		function new_data(){
		$.ajax({
             type: "post",
             dataType:'text',             
             url: "<%=path%>/jydz/findReFriends.action",
             success: function(data){
                 var zNodes = data;
                 $.fn.zTree.init($("#treeDemo2"), setting, eval('(' + zNodes + ')')); 
                }  
              });
		}
		/*初始化*/
		$(function(){
		    $("#but1").button(); 
            $("#but2").button();
		    old_data();
            new_data();
		});
		
		/*搜索ztree*/
		function searchZtree(){		    
            //alert("aa");
             var usermc =  encodeURI($("#usermc").val());             
             var url = "<%=path%>/jydz/findFriends.action?nodeid="+usermc;                    
             $.post(url,function(data) {
               var zNodes = data;                              
               $.fn.zTree.init($("#treeDemo"), setting, eval('(' + zNodes + ')'));              
            });
		}
		//-->
	</SCRIPT>
</HEAD>

  
  <body>
  <div id="best1">
<h2><span>定制协商交易对象</span></h2>




	<div class="content_wrap" >
	
	<div class="zTreeDemoBackground left">
	   <ul class="list">
          <li class="title">协商交易对象名称：<input id="usermc" type="text" style="width:320px;"/>
		&nbsp;<a id="menuBtn" href="#" onclick="searchZtree();">查询</a></li>
		
        </ul>
		<ul id="treeDemo" class="ztree"></ul>
	</div>
	
	<div class="center">
	<input type="button" id="but1" value=">" onclick="chickDrop('treeDemo','1');"/>
	<br>	
	<input type="button" id="but2" value="<" onclick="chickDrop('treeDemo2','2');"/>
    </div>
   	
	<div class="right">
	    <ul>&nbsp;</ul>
		<ul id="treeDemo2" class="ztree"></ul>
	</div>		
	<div>

</div>
	
</div>		
<div style="float:left;">
	<font color="red">[友情提示]：左侧窗口为被选项，右侧窗口为定制项，通过点击中间按钮或双击鼠标左键操作</font>
</div>
<br class="spacer" />

</div>
 
  </body>
</html>
